package prog1.serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 09/12/13
 */
public class TextSerializer implements ISerializer {
    @Override
    public void serialize(Map<String, String> data, String filename) throws Exception {
        Properties properties = new Properties();

        for (Map.Entry<String, String> entry : data.entrySet()) {
            properties.setProperty(entry.getKey(), entry.getValue());
        }

        properties.store(new FileOutputStream(filename), null);
    }

    @Override
    public void deserialize(Map<String, String> data, String filename) throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream(filename));

        Set<String> names = properties.stringPropertyNames();
        for (String name : names) {
            String value = properties.getProperty(name);
            data.put(name, value);
        }
    }
}
